let db =require('../db');

//登录界面token 存入数据库

exports.insertToken  = async function (token,tokenDate) {
    let sql=`
    INSERT  INTO login_token(token,createdAt)
    VALUES(:token,:tokenDate)
    `
    let data  =await db.query(sql,{token,tokenDate});
    return data[0];    
    }


//验证login token  
exports.validationToken  =  async function(token){
    let sql=`
    SELECT * FROM  login_token  where token =:token
    `
    let data = await db.query(sql,{token});
    return data[0];
}

//验证用户是否存在
exports.validationUser = async function(userName){
 let sql =`
 SELECT * FROM  admin WHERE userName=:userName
 `
 let data  = await db.query(sql,{userName});
 return data[0];
}
//添加token

exports.updateToken = async function(id,token){
    let sql=`
    UPDATE  admin SET 
    token =:token
    where id =:id
    `
    let data = await db.query(sql,{id,token});
    return data[0];
   }
   
   
   //验证api_token 的有效性

   exports.validationApiToken  = async function(id,api_token){
       let sql=`
       select * from  admin  where token =:api_token AND id=:id
       `
       let data = await db.query(sql,{id,api_token});
       return data[0];
   }